﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using Xenta.Utils;
using Xenta.Web.Controllers;

namespace Xenta.Web.Areas.Blog.Controllers
{
    public sealed class TagController : ExtController
    {
        [ChildActionOnly]
        public ActionResult _Cloud()
        {
            var m = Svc.ProcessOperation("SearchPublicationTags", new
            {
                LanguageID = JDM.Int32(Svc.Localization.Language.EntityID),
                FlagMask = "Active"
            }, new
            {
                DisclosureLevel = "Base",
                DisclosureOptions = new Dictionary<String, String>
                {
                    { "TranslatedTo", JDM.String(Svc.Localization.Language.Code) }
                }.Unload()
            }).Items;
            return PartialView(m);
        }

        [HttpPost]
        public ActionResult Autocomplete(string term)
        {
            var m = Svc.ProcessOperation("GetPublicationTagDictionary", new
            {
                LanguageID = JDM.Int32(Svc.Localization.Language.EntityID),
                Term = new
                {
                    Expression = String.Format("{0}%", term)
                },
                Count = 10
            });
            var data = new List<Object>();

            foreach(var i in m.Items)
            {
                data.Add(new
                {
                    id = JDM.String(i.Key),
                    value = JDM.String(i.Value)
                });
            }
            return Json(data.ToArray());
        }
    }
}
